package bdd.daos.impl;

import bdd.beans.Jours;
import bdd.beans.Reunion;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import beans.*;
import bdd.daos.*;
import bdd.daos.impl.DAOSalleImpl;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Set;

public class DAOReunionImpl implements DAOReunion {

	private DBLink dblink;

	public DAOReunionImpl() {		
		super();
		this.dblink = new DBLink(); 
	}
	@Override
	public Reunion save(Reunion reu) {
		try {
			dblink.connexion();
			this.dblink.setPst(this.dblink.getConnection().prepareStatement("INSERT INTO REUNION (numsalle, libelle, horairedebut, horairefin, iddate)" +
					" VALUES ("+reu.getSalle().getNumsalle()+",'"+reu.getLibelle()+"',"+reu.getHorairedebut()+","+reu.getHorairefin()+","+reu.getJours().getIddate()+")"));
			this.dblink.getPst().executeQuery();
			DAOOutils outil = new DAOOutils();
			reu.setNumreunion(outil.findMaxId("REUNION", "NUMREUNION"));
		}
		catch(SQLException se) {
			System.out.println("Erreur de requ�te");
		}
		finally{
			dblink.deconnexion();
		}
		return reu;
	}

	@Override
	public Reunion find(Integer id) {
		Reunion uneReunion = new Reunion();
		uneReunion.setNumreunion(id);
		
		try {
			dblink.connexion();
			this.dblink.setPst(this.dblink.getConnection().prepareStatement("SELECT * FROM REUNION WHERE NUMREUNION = " + id));
			ResultSet rs = this.dblink.getPst().executeQuery();
			while(rs.next()) {
				uneReunion.setSalle(new DAOSalleImpl().find(rs.getInt("NUMSALLE")));
				uneReunion.setHorairedebut(rs.getInt("HORAIREDEBUT"));
				uneReunion.setHorairefin(rs.getInt("HORAIREFIN"));
				uneReunion.setJours(new Jours(rs.getDate("IDDATE")));
				uneReunion.setLibelle(rs.getString("LIBELLE"));
			}
		}
		catch(SQLException se) {
			System.out.println("Erreur de requ�te");
		}
		finally{
			dblink.deconnexion();
		}
		return uneReunion;
	}

	public List<Reunion> findAll() {
		List<Reunion> Lstretour = new ArrayList<Reunion>();
		try {
			this.dblink.connexion();
			this.dblink.setPst(this.dblink.getConnection().prepareStatement("SELECT * FROM REUNION"));
			ResultSet rs = this.dblink.getPst().executeQuery();
			while(rs.next()) {
				Reunion retour = new Reunion();
                                retour.setNumreunion(rs.getInt("NUMREUNION"));
				retour.setSalle(new DAOSalleImpl().find(rs.getInt("NUMSALLE")));
				retour.setHorairedebut(rs.getInt("HORAIREDEBUT"));
				retour.setHorairefin(rs.getInt("HORAIREFIN"));
				retour.setJours(new Jours(rs.getDate("IDDATE")));
				retour.setLibelle(rs.getString("LIBELLE"));
				Lstretour.add(retour);
			}
		}
		catch(SQLException se) {
			System.out.println("Erreur de requête" + se);
		}
		finally{
			this.dblink.deconnexion();
		}
		return Lstretour;
	}

	public void update(Reunion o) {
		try {
			this.dblink.connexion();
			DateFormat format = new SimpleDateFormat("dd-MM-yy");
			String r = "UPDATE REUNION SET NUMSALLE='"+o.getSalle().getNumsalle()+ "', LIBELLE='"+o.getLibelle()+ "', HORAIREDEBUT='"+o.getHorairedebut()+ "', HORAIREFIN='"+o.getHorairefin()+ "', IDDATE='"+format.format(o.getJours().getIddate())+ "' WHERE NUMREUNION = " + o.getNumreunion();

			this.dblink.setPst(this.dblink.getConnection().prepareStatement(r));
			this.dblink.getPst().executeQuery();
		}
		catch(SQLException se) {
			System.out.println("Erreur de requ�te : " + se);
		}
		finally{
			this.dblink.deconnexion();
		}
	}

	@Override
	public void delete(Reunion persistentObject) {
		try {
			dblink.connexion();
			this.dblink.setPst(this.dblink.getConnection().prepareStatement("DELETE FROM REUNION WHERE NUMREUNION = " + persistentObject.getNumreunion()));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			dblink.deconnexion();
		}
	}

    public Set<Reunion> findByJour(Date jour) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Set<Reunion> findBySalle(Integer numSalle) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
